Given an array of strings, group anagrams together.

For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:

[ ["ate", "eat","tea"], ["nat","tan"], ["bat"] ] Note: All inputs will be in lower-case.


In [1]:
class Solution(object):
    def groupAnagrams(self, strs):
        """
        :type strs: List[str]
        :rtype: List[List[str]]
        """
        str_dict = {}
        for s in strs:
            tmp = "".join(sorted(s))
            if tmp in str_dict:
                str_dict[tmp].append(s)
            else:
                str_dict[tmp] = [s]
        return str_dict.values()

一遍就通过了


In [ ]: